package com.tanhua.server.controller;




import com.tanhua.model.vo.PageResult;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     *  更新用户头像
     * @param headPhoto 头像图片
     */
    @PostMapping("header")
    public ResponseEntity header(MultipartFile headPhoto){
        Long userId = UserHolder.getUserId();
        try {
            userInfoService.updateHeadPhoto(headPhoto, userId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
    }


    //修改手机号发送验证码
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendCode(){
        Long loginId = UserHolder.getUserId();
        userInfoService.sendCode(loginId);
        return ResponseEntity.ok(null);
    }

    /**
     * 检验登录
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String mobile = UserHolder.getMobile();
        String verificationCode = (String) map.get("verificationCode");
        Map<String, Boolean> booleanMap = userInfoService.loginVerification(verificationCode, mobile);
        return ResponseEntity.ok(booleanMap);
    }

    //修改手机号
    @PostMapping("/phone")
    public ResponseEntity updatePhoneNum(@RequestBody Map<String, String> map){
        Long userId = UserHolder.getUserId();
        String phone = map.get("phone");
        userService.updatePhone(userId, phone);
        return ResponseEntity.ok(null);
    }

    //查询互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Long userId = UserHolder.getUserId();
        Map<String, Integer> counts = userInfoService.counts(userId);
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/friends/{type}")
    public PageResult friendsType(@PathVariable Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname) {

        PageResult result = userInfoService.friendsType(page, pagesize, nickname, type);
        return result;
    }


    /**
     * 粉丝 - 喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable Long uid) {
        userInfoService.fansLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 粉丝 - 不喜欢
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity removeLike(@PathVariable Long uid) {
        userInfoService.removeLike(uid);
        return ResponseEntity.ok(null);
    }
}
